// 配置数据库信息
const usersql = require('../../mysql.js')
// 生成随机id
const setId = require('../../assets/id.js')

// 查询所有设备
const getEquip = (req, res) => {
    const pagemsg = req.query
    let sql = 'select * from equipment where 1=1'
    if (req.query.details) sql = sql + " and details like '%" + req.query.details + "%'"
    if (req.query.name) sql = sql + " and name like '%" + req.query.name + "%'"
    usersql.query(sql,(err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            var p = pagemsg.pagesize*pagemsg.page
            var arr = results.slice(p - pagemsg.pagesize, p)
            res.send({ status: 200, message: '成功', data: { rows: arr, total: results.length } })
        } else {
            return res.cc('获取设备列表失败')
        }
    })
}


// 添加设备
const addEquip =  (req, res) => {
    // studentid studentname classname college password
    const newequip = req.body
    newequip.id = setId(10000000,99999999)
    newequip.createtime = new Date ()
    const sql = 'insert into equipment set ?'
    usersql.query(sql, newequip, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) {
            return res.cc('添加失败，请稍后再试')
        }
        res.send({ status: 200, message: '添加设备成功' })
    })
}

// 删除设备
const deleteEquip =  (req, res) => {
    // studentid studentname classname college password
    const id = req.query.id
    const sql = 'delete from equipment where id=?'
    usersql.query(sql, id, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 0) {
            res.send({ status: 200, message: '删除成功' })
        } else {
            return res.cc('删除失败')
        }
    })
}

// 编辑设备
const putEquip = (req, res) => {
    const equip = req.body
    const sql = 'update equipment set ? where id=?'
    usersql.query(sql, [equip,equip.id], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 0) {
            res.send({ status: 200, message: '编辑成功', data: results })
        } else {
            return res.cc('编辑失败')
        }
    })
}

// 获取设备详情信息
const getEquipDetail = (req, res) => {
    const id = req.query.id
    const sql = 'select * from equipment where id=?'
    const sql2 = 'select * from equipLend where equipid=?'
    usersql.query(sql, id,(err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            let data = results[0]
            usersql.query(sql2, id,(err, results2) => {
                if (err) return res.cc(err)
                if (results2.length !== 0) {
                    data.lendHistory = results2;
                    res.send({ status: 200, message: '查询设备成功', data })
                } else {
                    data.lendHistory = [];
                    res.send({ status: 200, message: '查询设备成功', data })
                }
            })
        } else {
            return res.cc('获取失败')
        }
    })
}

// 租借设备
const equipLend = (req, res) => {
    const newequiplend = req.body
    newequiplend.estatus == 0
    newequiplend.lendid = setId(10000000,99999999)
    newequiplend.progress = 0
    const sql = 'insert into equipLend set ?'
    usersql.query(sql, newequiplend, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) {
            return res.cc('添加失败，请稍后再试')
        } else {
            res.send({ status: 200, message: '租借信息提交设备成功'})
            
            
        }
    })
    
}
// 归还设备
const equipReturn = (req, res) => {
    let id = req.query.id
    let equipid = req.query.equipid
    let newequiplend = {}
    newequiplend.estatus == 1
    newequiplend.returntime = new Date ()
    newequiplend.progress = 2
    const sql2 = 'update equipLend set ? where lendid=?'
    usersql.query(sql2, [newequiplend,id], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 0) {
            res.send({ status: 200, message: '归还信息提交成功，请等待管理员审核', data: results })
        } else {
            return res.cc('归还失败，请联系管理员')
        }
    })
    
}



// 查询所有设备租借记录
const getEquipHistory = (req, res) => {
    let pagemsg = req.query
    let sql = 'select * from equipment,equipLend where id=equipid'
    if (req.query.progress) sql = sql + " and progress='" + req.query.progress + "'"
    if (req.query.name) sql = sql + " and name like '%" + req.query.name + "%'"
    if (req.query.student) sql = sql + " and student like '%" + req.query.student + "%'"
    usersql.query(sql,(err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            var p = pagemsg.pagesize*pagemsg.page
            var arr = results.slice(p - pagemsg.pagesize, p)
            res.send({ status: 200, message: '成功', data: { rows: arr, total: results.length } })
        } else {
            res.send({ status: 200, message: '无租借记录'})
        }
    })
}
// 查询所有设备租借记录
const getEquipHistory2 = (req, res) => {
    const equipid = req.query.id
    let sql = 'select * from equipment,equipLend where id=equipid and equipid=?'
    usersql.query(sql,equipid,(err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            var p = pagemsg.pagesize*pagemsg.page
            var arr = results.slice(p - pagemsg.pagesize, p)
            res.send({ status: 200, message: '成功', data: { rows: arr, total: results.length } })
        } else {
            res.send({ status: 200, message: '无租借记录' })
        }
    })
}

// 查询所有待审核设备
const getEquipHistory3 = (req, res) => {
    let pagemsg = req.query
    let sql = 'select * from equipment,equipLend where (id=equipid) and (progress=0 or progress=2) '
    if (req.query.name) sql = sql + " and name like '%" + req.query.name + "%'"
    if (req.query.student) sql = sql + " and student like '%" + req.query.student + "%'"
    usersql.query(sql,(err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            var p = pagemsg.pagesize*pagemsg.page
            var arr = results.slice(p - pagemsg.pagesize, p)
            res.send({ status: 200, message: '成功', data: { rows: arr, total: results.length } })
        } else {
            res.send({ status: 200, message: '暂无待审核申请'})
        }
    })
}
// 审核设备租借信息
const auditEquipLend = (req, res) => {
    // progress: 1审核通过、-1审核未通过、3已归还
    const id = req.query.id
    const equipid = req.query.equipid
    const progress = Number(req.query.progress)
    let obj = progress==1? { progress, estatus: 0, lendtime: new Date () } : { progress, estatus: 1 }
    let sql = 'update equipLend set? where lendid=?'
    const sql3 = 'update equipment set num=num-1 where id=?'
    const sql2 = 'update equipment set num=num+1 where id=?'

    usersql.query(sql,[obj, id],(err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 0) {
            if (progress == 1) {
                usersql.query(sql3, equipid, (err, results2) => {
                    if (err) return res.cc(err)
                    if (results.affectedRows !== 0) {
                        res.send({ status: 200, message: '租借信息审核成功'})
                    } else {
                        return res.cc('审核失败，请联系管理员')
                    }
                })
            } else if(progress == -1||progress == -2) {
                res.send({ status: 200, message: '审核未通过' })
            } else {
                usersql.query(sql2, equipid, (err, results2) => {
                    if (err) return res.cc(err)
                    if (results.affectedRows !== 0) {
                        res.send({ status: 200, message: '审核成功，设备已归还', data: results })
                    } else {
                        return res.cc('审核失败')
                    }
                })
            }
        } else {
            return res.cc('审核失败')
        }
    })
}
// 根据用户id获取租借历史
const getUserLend = (req, res) => {
    const student = req.query.student
    let sql = 'select * from equipment,equipLend where id=equipid and student=?'
    usersql.query(sql,student,(err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            res.send({ status: 200, message: '成功', data: results })
        } else {
            res.send({ status: 200, message: '无租借记录', data: [] })
        }
    })
}

// 撤销租借或删除租借信息
const deletetUserLend = (req, res) => {
    const id = req.query.id
    const sql = 'delete from equipLend where lendid=?'
    usersql.query(sql,id,(err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 0) {
            res.send({ status: 200, message: '成功' })
        } else {
            return res.cc('失败')
        }
    })
}

// 查看租借详情信息
const getUserLendDetail = (req, res) => {
    const id = req.query.id
    let sql = 'select * from equipment,equipLend where id=equipid and lendid=?'
    usersql.query(sql,id,(err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 0) {
            res.send({ status: 200, message: '成功', data: results })
        } else {
            return res.cc('失败')
        }
    })
}



module.exports = { getEquip, addEquip, deleteEquip, putEquip, getEquipDetail, getEquipHistory, equipLend, equipReturn, auditEquipLend, getUserLend, getEquipHistory2, getUserLendDetail, deletetUserLend, getEquipHistory3 }
